var mongodb = require('mongodb');

var client = new mongodb.Db('myrpg', new mongodb.Server("127.0.0.1", 27017, {}));

var users = null;
var characters = null;

client.open(function(err, p_client) {
    if(err)
        console.warn(err.message);
    else{
        p_client.collection('users', function (err, collection) {
            users = collection;
            exports.disconnectAll();
        });
        p_client.collection('characters', function (err, collection) {
            characters = collection;
            exports.resetCharacters();
        });
    }  
});

exports.getAll = function(callback){
    users.find().toArray(callback);
};

exports.register = function(user, callback){
    users.find({
        username:user.username
    }).nextObject(function(err, object){
        if(err){
            callback(err);
        }else if(object){
            var error = {};
            error.message = "duplicate user error: " + user.username;
            callback(error);
        }else{
            users.insert(user, {
                safe:true
            }, function(err, objects){
                if(err)
                    callback(err);
                else
                    callback(null, objects[0]);
            });
        }
    }); 
}

exports.login = function(user, sessionId, callback){
    users.find({
        username:user.username, 
        password:user.password
    }).nextObject(function(err, obj){
        if(err){
            callback(err);
        }else{
            if(obj){               
                users.findAndModify({
                    username:obj.username
                },[],{
                    $set:{
                        sessionId: sessionId
                    }
                },{
                    'new':true
                }, callback);
            }else{
                var error = {};
                error.message = "not registered";
                callback(error);
            }
            
        }
    });
}

exports.disconnect = function(username){
    users.update({
        username:username
    },{
        $set:{
            connected:false
        }
    });
}

exports.disconnectAll = function(){
    users.update({
        _id: {
            $exists : true
        }
    },{
        $set:{
            connected:false
        }
    }, {
        multi:true
    });
}

exports.setCharacter = function(user, character, callback){

    users.update({
        username:user.username
    }, {
        $set:{
            character:character
        }
    }, {
        safe:true
    }, callback);
}

exports.getCharacter = function(sessionId, callback){
    users.findAndModify({
        sessionId:sessionId
    },[],{
        $set:{
            connected: true
        }
    },{
        'new':false
    }, function(err, obj){
        if(err){
            callback(err);
        }else if(obj){
            if(obj.connected == false){
                characters.find({
                    name:obj.character
                }).nextObject(function(err, character){
                    if(err)
                        callback(err);
                    else{
                        callback(null, character);
                    }
                });
            }else{
                var error = {};
                error.message = "already connected";
                callback(error);
            }
        }else{
            callback(null, null);
        }
    });
};

exports.resetCharacters = function(){
    characters.update({
        _id: {
            $exists : true
        }
    },{
        $set:{
            kills:0,
            deaths:0
        }
    }, {
        multi:true
    });
};
